package com.example.yc23.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yc23.demo.pojo.Film;
import com.example.yc23.demo.pojo.Occupation;
import com.example.yc23.demo.service.FilmService;
import com.example.yc23.demo.service.MakerService;
import com.example.yc23.demo.service.OccupationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/film")
@Controller
public class FilmController {
    @Autowired
    private FilmService filmService;
    @Autowired
    private MakerService makerService;
    @Autowired
    private OccupationService occupationService;

    /**
     * 实现电影信息的分页查询
     * @param key 查询关键字
     * @param pn    页码
     * @param ps    每页显示数据条数
     * @return
     */
    //跳转到电影管理首页
    @GetMapping({"/index","/index.html"})
    public String filmIndex(String key, @RequestParam(defaultValue = "1") Integer pn,@RequestParam(defaultValue = "8")Integer ps,Model model) {
        Page<Film> page=new Page<>(pn,ps);//创建一个分页组件
        QueryWrapper wrapper=null;
        if (key!=null && !"".equals(key)){
            wrapper=new QueryWrapper();
            wrapper.like("name",key);
            wrapper.or();
            wrapper.like("another_name",key);
        }
        model.addAttribute("page",filmService.page(page,wrapper));
        return "admin/film/index";
    }
    @GetMapping({"/add","/update"})
    public String filmadd(Integer id,Model model) {
        if (id!=null){
            model.addAttribute("film",filmService.getById(id));
        }
        return "admin/film/updateoradd";
    }
    @PostMapping("/updateoradd")
    public String updateoradd(Film film, Model model){
        if (filmService.saveOrUpdate(film)){
            return "redirect:/admin/film/index";//操作成功回到电影管理页面
        }else{
            model.addAttribute("film",film);
            model.addAttribute("msg","操作失败！");
            return "admin/film/updateoradd";//失败回到操作页面
        }
    }
    @GetMapping("/cast-member/{filmId}")
    public String castMember(@PathVariable Integer filmId, Model model){
        model.addAttribute("film",filmService.getById(filmId));
        return "admin/film/cast-member";
    }
    @PostMapping("/cast-member")
    public String castMember(String makerName,Integer filmId, Model model){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("name",makerName);
        List<Occupation> occupationList=occupationService.list();
        model.addAttribute("occuList",occupationList);
        model.addAttribute("makerList",makerService.list(wrapper));
        return castMember(filmId,model);
    }

    /**
     *  将客户端发送的数据添加到数据库中
     * @param filmId
     * @param makerId
     * @param job
     * @return
     */
    @PostMapping("/cast-member/add")
    @ResponseBody
    public Map add(Integer filmId,Integer makerId,String job){
        return filmService.addCastMember(filmId,makerId,job);
    }
    }
